package com.situ.player.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.situ.player.R;
import com.situ.player.activity.MainActivity;
import com.situ.player.activity.MusicPlayActivity;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.StringTokenizer;
import java.util.concurrent.Future;

import static android.R.id.list;

/**
 * Created by Gao on 2022/6/13.
 */
public class FileAdapter extends BaseAdapter{
    // 当前要展示的目录下面所有文件和文件夹
    private File[] mFiles;
    private MainActivity mMainActivty;

    public FileAdapter(MainActivity mainActivity) {
        this.mMainActivty = mainActivity;
    }

    /**
     *
     * @param curentDirectory 当前目录
     */
    public void updateData(File curentDirectory) {
        //mFiles = curentDirectory.listFiles();
        mFiles = curentDirectory.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return !pathname.getName().startsWith(".");
            }
        });

        Collections.sort(Arrays.asList(mFiles), new Comparator<File>() {
            @Override
            public int compare(File file1, File file2) {
                if (file1.isDirectory() && file2.isFile()) {
                    return -1;
                } else if (file1.isFile() && file2.isDirectory()) {
                    return 1;
                } else {
                    // file1和file2要么同时是文件夹要么同时是文件
                    // 字符串已经默认实现了compareTo方法，按照字母顺序排序
                    return file1.getName().compareTo(file2.getName());
                }
            }
        });

        notifyDataSetChanged();


    }

    @Override
    public int getCount() {
        return mFiles.length;
    }

    @Override
    public Object getItem(int position) {
        return mFiles[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    // 处理Data-View数据绑定过程
    @Override
    public View getView(int position, View convertView, final ViewGroup parent) {
        ViewHolder viewHolder = new ViewHolder();
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) mMainActivty.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.list_item, null);
            viewHolder.ivFileIcon = (ImageView) convertView.findViewById(R.id.iv_file_icon);
            viewHolder.tvFileName = (TextView) convertView.findViewById(R.id.tv_file_name);
            // 第一次保存起来
            convertView.setTag(viewHolder);
        } else {
            // 不是第一次直接拿出来
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final File file = mFiles[position];
        final String fileName = file.getName();// aaa.mp3
        viewHolder.tvFileName.setText(fileName);
        if (file.isDirectory()) {
            viewHolder.ivFileIcon.setBackgroundResource(R.drawable.icon_folder);
        } else {
            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);// mp3
            if ("mp3".equalsIgnoreCase(suffix)) {
                viewHolder.ivFileIcon.setBackgroundResource(R.drawable.icon_audio);
            } else if ("mp4".equalsIgnoreCase(suffix)) {
                viewHolder.ivFileIcon.setBackgroundResource(R.drawable.icon_video);
            } else {
                viewHolder.ivFileIcon.setBackgroundResource(R.drawable.icon_file);
            }

        }

        // 添加点击事件
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (file.isDirectory()) {
                    updateData(file);
                    mMainActivty.updateCurrentDirectory(file);
                    // 跟新了Adapter中数据，需要调用这个方法
                    // notifyDataSetChanged();
                } else {
                    String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
                    if ("mp3".equalsIgnoreCase(suffix)) {
                        Intent intent = new Intent(mMainActivty, MusicPlayActivity.class);
                        intent.putExtra("path", file.getAbsolutePath());// 拿到文件的路径
                        // 拿到当前目录下面所有mp3为结尾歌曲
                        ArrayList<String> mp3List = getMp3List(new File(file.getParent()));
                        intent.putExtra("mp3List", mp3List);
                        mMainActivty.startActivity(intent);
                    } else {

                    }
                }
            }

            public ArrayList<String> getMp3List(File currentDirectory) {
                File[] list = currentDirectory.listFiles(new FileFilter() {
                    @Override
                    public boolean accept(File pathname) {
                        return pathname.getName().endsWith(".mp3");
                    }
                });
                ArrayList<String> mp3List = new ArrayList<String>();
                for (File file : list) {
                    mp3List.add(file.getAbsolutePath());
                }
                return mp3List;
            }

        });


        return convertView;
    }

    public class ViewHolder{
        public ImageView ivFileIcon;
        public TextView tvFileName;
    }
}
